//
//  CarInputChooseModelVC.m
//  New_Runway
//
//  Created by mack on 2018/3/15.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarInputChooseModelVC.h"
#import "CarVersionModel.h"
#import "CarInputChooseModel.h"

@interface CarInputChooseModelVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *carVersionArray;

@end

@implementation CarInputChooseModelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"车型";
    self.view.backgroundColor= kColorWhite();
    [self initTableview];
//    [self loadCarModel:self.modelId];
    [self loadCarModel:self.model.seriesId];
    
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    //    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    self.tableview.tableFooterView = [[UIView alloc] init];
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.carVersionArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.tintColor = MAIN_NAV_COLOR();
    cell.textLabel.textColor = MAIN_FONT_COLOR();
    [cell.textLabel setFont:kFontSize(14)];
    versionResult *versionInfo = self.carVersionArray[indexPath.row];
    cell.textLabel.text = versionInfo.modelName;
    cell.textLabel.numberOfLines = 2;
    if (self.seriesIndexPath == indexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor = MAIN_NAV_COLOR();
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *celled = [tableView cellForRowAtIndexPath:self.seriesIndexPath];
    celled.accessoryType = UITableViewCellAccessoryNone;
    celled.textLabel.textColor = MAIN_FONT_COLOR();
    //记录当前选中的位置索引
    self.seriesIndexPath = indexPath;
    //当前选择的打勾
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.textLabel.textColor = MAIN_NAV_COLOR();
    [self.tableview reloadData];
    versionResult *versionInfo = self.carVersionArray[indexPath.row];
    self.model.modelName = versionInfo.modelName;
    self.model.modelId = versionInfo.modelId;
//    NSDictionary *dic = @{
//                              @"brandId":@(self.carBrandId),
//                              @"modelId":@(self.modelId),
//                              @"seriesId":@(versionInfo.modelId),
//                              @"brandName": self.carBrandName,
//                              @"modelName": self.modelName,
//                              @"seriesName": versionInfo.modelName
//                              };
        NSNotification *notificatiom = [NSNotification notificationWithName:@"ChooseCar" object:self.model userInfo:nil];
        [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
    
//    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.tabIndex] animated:YES];
    
    [self.navigationController safePopViewControllerWithClass:self.fromControllerClass];

    [self.tableview reloadData];
    
}

//获取车型
-(void)loadCarModel:(NSInteger)seriesId{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,CarModelApi];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:@{@"seriesId":@(seriesId)} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CarVersionModel *model = [CarVersionModel mj_objectWithKeyValues:response];
        if (model.code == 1) {
            [self.carVersionArray removeAllObjects];
            for (NSDictionary *dic in model.result) {
                versionResult *versionInfo = [versionResult mj_objectWithKeyValues:dic];
                [self.carVersionArray addObject:versionInfo];
            }
            
        }
        [self.tableview reloadData];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
}
-(NSMutableArray *)carVersionArray{
    if (!_carVersionArray) {
        _carVersionArray = [[NSMutableArray alloc] init];
    }
    return _carVersionArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
